﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace HSDc.RingleFlow.Kernel.DBPersit.Dao.Util
{
    internal class ConnectionFactory
    {
        private static ConnectionFactory instance = null;

        internal static ConnectionFactory GetInstance()
        {
            if (instance == null)
                instance = new ConnectionFactory();
            return instance;
        }

        private ConnectionFactory() { }
        private RingleFlowEntities entity = null;
        internal RingleFlowEntities GetEntities()
        {
            string connStrPath = this.GetType().Assembly.CodeBase.Substring(8).Replace("/", "\\");
            if (entity != null)
            {
                entity.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                //entity.Dispose();
            }
            entity = new RingleFlowEntities(ConfigurationManager.OpenExeConfiguration(connStrPath).ConnectionStrings.ConnectionStrings["RingleFlowEntities"].ConnectionString);
            return entity;
        }

        internal void CloseConnection()
        {
            if (entity != null)
                entity.Dispose();
        }
    }
}
